package com.sec.android.app.myfiles.decorator.privateinfo;

import android.content.Context;
import android.view.View;
import android.view.ViewStub;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.decorator.AbsAdapterImpDecorator;
import com.sec.android.app.myfiles.fragment.filelist.FileListAdapter;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.util.FileUtils;
import com.sec.android.app.myfiles.widget.listview.FileListViewHolder;
import com.sec.android.app.myfiles.widget.listview.ListViewHolder;

/**
 * Created by jw7.jeong on 2016-06-07.
 */
public class PrivateAdapterDecorator extends AbsAdapterImpDecorator {

    public PrivateAdapterDecorator(Context context, FileListAdapter adapter) {
        super(context, adapter);
    }

    @Override
    protected void _bindCreateFolderView(ListViewHolder vh, View v) {
        // do nothing
    }

    @Override
    protected void _createViewHolder(View v, ListViewHolder vh) {
        vh.addView(FileListViewHolder.PRIVATE_ICON_STUB, v.findViewById(R.id.private_icon_stub));
    }

    @Override
    protected void _bindView(Context context, ListViewHolder vh, View v, FileRecord record, int position) {
        _bindPrivateIcon(vh, v, record);
    }

    @Override
    protected void _bindProgressSync(ListViewHolder vh, View v, FileRecord record, boolean isShow) {
        // do nothing
    }

    private void _bindPrivateIcon(ListViewHolder vh, View v, FileRecord record) {
        boolean isPrivate = FileUtils.isPrivateFolder(record.getFullPath());

        if ((FileRecord.StorageType.Category == record.getStorageType()) || (!record.isDirectory())) {
            showPrivateIcon(vh, v, isPrivate);
        } else {
            showPrivateIcon(vh, v, false);
        }
    }

    public static void showPrivateIcon(ListViewHolder vh, View v, boolean show) {
        View privateIcon = vh.getView(FileListViewHolder.PRIVATE_ICON);
        if (show && privateIcon == null) {
            privateIcon = ensurePrivateIcon(vh, v);
        }
        if (privateIcon != null) {
            privateIcon.setVisibility(show ? View.VISIBLE : View.GONE);
        }
    }

    private static View ensurePrivateIcon(ListViewHolder vh, View v) {
        View ret;
        ViewStub stub;
        stub = vh.getView(FileListViewHolder.PRIVATE_ICON_STUB, ViewStub.class);
        if (stub != null) {
            ret = stub.inflate();
        } else {
            ret = v.findViewById(R.id.private_icon);
        }

        if (ret != null) {
            vh.addView(FileListViewHolder.PRIVATE_ICON, ret);
        }
        return ret;
    }
}
